#include "base.h"
#include "algorithm"

vector<vector<int>> sum_three_zero(vector<int> nums)
{
    std::sort(nums.begin(), nums.end());
    vector<vector<int>> final;
    for (int i = 0; i < nums.size(); i++)
    {
        if (i > 0 && nums[i] == nums[i - 1])
        {
            continue;
        }
        int j = i + 1, q = nums.size() - 1;
        while (q > j)
        {
            if (nums[j] + nums[q] < -nums[i])
            {
                j++;
                while (nums[j] == nums[j - 1] && q > j)
                {
                    j++;
                }
            }
            else
            {
                if (nums[j] + nums[q] + nums[i] == 0)
                {
                    vector<int> a(3);
                    a[0] = nums[i];
                    a[1] = nums[j];
                    a[2] = nums[q];

                    final.push_back(a);
                }
                q--;
                while (nums[q] == nums[q + 1] && q > j)
                {
                    q--;
                }
            }
        }
    }

    return final;
}

int main()
{
    vector<int> a = {-1, 0, 1, 2, -1, -4};
    auto res = sum_three_zero(a);

    return 0;
}